package com.tzsafe.nfcdemo

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.hjy.bluetooth.HBluetooth
import com.hjy.bluetooth.entity.BluetoothDevice
import com.hjy.bluetooth.entity.ScanFilter
import com.hjy.bluetooth.inter.ScanCallBack
import com.tzsafe.nfcdemo.databinding.ActivityDeviceListBinding


class DeviceListActivity : AppCompatActivity(), View.OnClickListener {
    var isScanOver = false
    var deviceType = 2 //5 ：卢月亭的lora 6：打桩 7：无线传输工具设置 8：信号噪声扫描 9:曾波的4g钥匙 名字：TZSK
    var deviceFilterName = "TZSK"

    companion object {
        const val TAG = "DeviceListActivity"
    }
    private lateinit var mBind: ActivityDeviceListBinding
    private val deviceAdapter: DeviceAdapter by lazy { DeviceAdapter() }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBind = ActivityDeviceListBinding.inflate(layoutInflater)
        setTitle("搜索蓝牙设备"); // 设置自定义标题文本
        setContentView(mBind.root)
        deviceType = intent.getIntExtra("deviceType", 2)
        deviceFilterName = if (deviceType == 2) "TZSK"
        else "TZWN23S"
        Log.e(TAG, "扫描类型 " + deviceType)
        //初始化 recycleView
        mBind.listRecyclerView.grid(1).divider {
            orientation = DividerOrientation.GRID
            includeVisible = true
            setDivider(1, true)
            setColor(getColorExt(R.color.common_text_hint_color))
        }.adapter = deviceAdapter
        scanWithTimeUse(2)
        deviceAdapter.setOnItemClickListener { adapter, view, position ->
            LiveDataEvent.connectBleDevice.value =
                BLEConnectDevice(adapter.data.get(position) as BluetoothDevice, true)
            DeviceListActivity@ this.finish()
        }
        mBind.btnScan.setOnClickListener {
            if (!isScanOver) {
                deviceAdapter.setNewData(ArrayList<BluetoothDevice>())
                deviceAdapter.notifyDataSetChanged()
            } else {
                deviceAdapter.setNewData(ArrayList<BluetoothDevice>())
                deviceAdapter.notifyDataSetChanged()
                scanWithTimeUse(2)
            }
        }
    }


    fun getColorExt(id: Int): Int = ContextCompat.getColor(appContext, id)

    lateinit var deviceFilter: ScanFilter
    private fun scanWithTimeUse(type: Int) {
        isScanOver = false
        Log.e("scanWithTimeUse", "过滤的名字 " + deviceFilterName)

        if (deviceType == 5) {
            deviceFilter = ScanFilter(true, "SPD")
        } else if (deviceType == 6) {
            deviceFilter = ScanFilter(true, "SPD")
        } else if (deviceType == 7) {
            deviceFilter = ScanFilter(true, "LRB", "VWl", "LZ", "LJZ")
        } else if (deviceType == 8) {
            deviceFilter = ScanFilter(true, "LRB")
        } else if (deviceType == 9) {
            deviceFilter = ScanFilter(true, "TZSK", "DH")
        } else {
            deviceFilter = ScanFilter(true, deviceFilterName, "VWK")
        }

        //扫描蓝牙设备，扫描6秒就自动停止扫描
        HBluetooth.getInstance().scan(type, 7000, deviceFilter, object : ScanCallBack {
            override fun onScanStart() {
                Log.e(TAG, "开始扫描")
            }

            override fun onScanning(
                scannedDevices: MutableList<BluetoothDevice>?,
                currentScannedDevice: BluetoothDevice?,
            ) {
                deviceAdapter.setNewData(scannedDevices)
                deviceAdapter.notifyDataSetChanged()
            }

            override fun onError(errorType: Int, errorMsg: String?) {
                Log.e(TAG, "errorType:" + errorType + "  errorMsg:" + errorMsg);
            }

            override fun onScanFinished(bluetoothDevices: MutableList<BluetoothDevice>?) {
                Log.e(TAG, "扫描结束")
//                ToastUtils.show("扫描结束")
                isScanOver = true
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun onClick(v: View?) {
    }
}